using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class MKhungDaoTaoMonDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the mKhungDaoTaoMon table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, MKhungDaoTaoMonDTO mKhungDaoTaoMon)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mKhungDaoTaoMon", mKhungDaoTaoMon);

			object[] parameters = new object[]
			{
				Null.GetNull(mKhungDaoTaoMon.KDT_ID, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.MO_ID, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.LoaiMon, ""),
				Null.GetNull(mKhungDaoTaoMon.LyThuyet, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.ThucHanh, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.SoDiemDinhKi, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.KiemTra, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.HeSoMon, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.TinhDiemTongKet, false)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mKhungDaoTaoMonInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mKhungDaoTaoMonInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mKhungDaoTaoMonInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the mKhungDaoTaoMon table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, MKhungDaoTaoMonDTO mKhungDaoTaoMon)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mKhungDaoTaoMon", mKhungDaoTaoMon);

			object[] parameters = new object[]
			{
				Null.GetNull(mKhungDaoTaoMon.KDT_ID, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.MO_ID, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.LoaiMon, ""),
				Null.GetNull(mKhungDaoTaoMon.LyThuyet, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.ThucHanh, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.SoDiemDinhKi, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.KiemTra, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.HeSoMon, DBNull.Value),
				Null.GetNull(mKhungDaoTaoMon.TinhDiemTongKet, false)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mKhungDaoTaoMonUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mKhungDaoTaoMonUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mKhungDaoTaoMonUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mKhungDaoTaoMon table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int kDT_ID, int mO_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(kDT_ID, DBNull.Value),
Null.GetNull(mO_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mKhungDaoTaoMonDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mKhungDaoTaoMonDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mKhungDaoTaoMonDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mKhungDaoTaoMon table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mKhungDaoTaoMonDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mKhungDaoTaoMonDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mKhungDaoTaoMonDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the mKhungDaoTaoMon table.
		/// </summary>
		public static MKhungDaoTaoMonDTO Select(Database objDb, int kDT_ID, int mO_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(kDT_ID, DBNull.Value),
Null.GetNull(mO_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mKhungDaoTaoMonSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the mKhungDaoTaoMon table.
		/// </summary>
		public static DataTable Select(Database objDb, int kDT_ID, int mO_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(kDT_ID, DBNull.Value),
Null.GetNull(mO_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mKhungDaoTaoMonSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the mKhungDaoTaoMon table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("mKhungDaoTaoMonSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the mKhungDaoTaoMon table.
		/// </summary>
		public static List<MKhungDaoTaoMonDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mKhungDaoTaoMonSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mKhungDaoTaoMon table.
		/// </summary>
		public static List<MKhungDaoTaoMonDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mKhungDaoTaoMonSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mKhungDaoTaoMon table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mKhungDaoTaoMonSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mKhungDaoTaoMon table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mKhungDaoTaoMonSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the MKhungDaoTaoMonDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static MKhungDaoTaoMonDTO MapDataReader(IDataReader dataReader)
		{
			MKhungDaoTaoMonDTO mKhungDaoTaoMonDTO = new MKhungDaoTaoMonDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return mKhungDaoTaoMonDTO;
			mKhungDaoTaoMonDTO.KDT_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["KDT_ID"]);
			mKhungDaoTaoMonDTO.MO_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["MO_ID"]);
			mKhungDaoTaoMonDTO.LoaiMon = clsCommon.fncCnvNullToString(dt.Rows[0]["LoaiMon"]);
			mKhungDaoTaoMonDTO.LyThuyet = clsCommon.fncCnvNullToInt(dt.Rows[0]["LyThuyet"]);
			mKhungDaoTaoMonDTO.ThucHanh = clsCommon.fncCnvNullToInt(dt.Rows[0]["ThucHanh"]);
			mKhungDaoTaoMonDTO.SoDiemDinhKi = clsCommon.fncCnvNullToInt(dt.Rows[0]["SoDiemDinhKi"]);
			mKhungDaoTaoMonDTO.KiemTra = clsCommon.fncCnvNullToInt(dt.Rows[0]["KiemTra"]);
			mKhungDaoTaoMonDTO.HeSoMon = clsCommon.fncCnvNullToInt(dt.Rows[0]["HeSoMon"]);
			mKhungDaoTaoMonDTO.TinhDiemTongKet = clsCommon.fncCnvNullToBool(dt.Rows[0]["TinhDiemTongKet"]);

			return mKhungDaoTaoMonDTO;
		}
		/// <summary>
		/// Creates a new instance of the MKhungDaoTaoMonDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<MKhungDaoTaoMonDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<MKhungDaoTaoMonDTO> lstResult = new List<MKhungDaoTaoMonDTO>();
			MKhungDaoTaoMonDTO mKhungDaoTaoMonDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			mKhungDaoTaoMonDTO = new MKhungDaoTaoMonDTO();
			mKhungDaoTaoMonDTO.KDT_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["KDT_ID"]);
			mKhungDaoTaoMonDTO.MO_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["MO_ID"]);
			mKhungDaoTaoMonDTO.LoaiMon = clsCommon.fncCnvNullToString(dt.Rows[i]["LoaiMon"]);
			mKhungDaoTaoMonDTO.LyThuyet = clsCommon.fncCnvNullToInt(dt.Rows[i]["LyThuyet"]);
			mKhungDaoTaoMonDTO.ThucHanh = clsCommon.fncCnvNullToInt(dt.Rows[i]["ThucHanh"]);
			mKhungDaoTaoMonDTO.SoDiemDinhKi = clsCommon.fncCnvNullToInt(dt.Rows[i]["SoDiemDinhKi"]);
			mKhungDaoTaoMonDTO.KiemTra = clsCommon.fncCnvNullToInt(dt.Rows[i]["KiemTra"]);
			mKhungDaoTaoMonDTO.HeSoMon = clsCommon.fncCnvNullToInt(dt.Rows[i]["HeSoMon"]);
			mKhungDaoTaoMonDTO.TinhDiemTongKet = clsCommon.fncCnvNullToBool(dt.Rows[i]["TinhDiemTongKet"]);
lstResult.Add(mKhungDaoTaoMonDTO);
}

			return lstResult;
		}

		#endregion
	}
}
